package main

import (
	"encoding/json"
	"fmt"
	"strings"
)

func main() {

	arr := [...]int{2: 4}
	fmt.Println("6arr=", arr)

	test123()

	foo1()
	fmt.Println("return", test())

	changeVar()

	buf, _ := json.Marshal(Stu{"Tom", "t001", 18})
	fmt.Printf("%s\n", buf)
}

func test123() {
	str := "Hello"
	fmt.Printf("%d World", str)
}

func foo1() {
	fmt.Println("Go 语言中，字符串是只读的，也就意味着每次修改操作都会创建一个新的字符串。\n如果需要拼接多次，应使用 strings.Builder，最小化内存拷贝次数。")

	var str strings.Builder
	for i := 0; i < 100; i++ {
		str.WriteString("a")
	}
	fmt.Println(str.String())
}

func test() int {
	i := 0
	defer func() {
		fmt.Println("defer1")
	}()
	defer func() {
		i += 1
		fmt.Println("defer2")
	}()
	return i
}

func changeVar() {
	a, b := "A", "B"
	fmt.Println("a=", a, "b=", b)
	a, b = b, a
	fmt.Println("a=", a, "b=", b)

}

type Stu struct {
	Name string `json:"stu_name"`
	ID   string `json:"stu_id"`
	Age  int    `json:"-"`
}
